package com.turing.post.common.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.CommonApplicationException;
import com.turing.post.common.util.LeftMenu;
import com.turing.post.common.util.LeftParentMenu;
import com.turing.post.common.util.SysOpMenu;
import com.turing.post.common.util.SysOpMenuDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "common.impl.SysOpMenuDaoImpl")
@Remote
@Local
public class SysOpMenuDaoImpl implements SysOpMenuDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BigInteger getMaxMenuId() {
		Query query = _em
				.createNativeQuery("select nvl(max(t.menuid),0)+1 menuid from sysopmenu t");

		return new BigInteger(String.valueOf(((BigDecimal) query
				.getSingleResult())));
	}

	// 单查询
	@Override
	public SysOpMenu getSysOpMenuByMenuId(Integer menuId) {
		return _em.find(SysOpMenu.class, menuId);
	}

	// 总数
	public BigInteger getSysOpMenuTotal(String menuName, String menuType) {
		String cond = "";
		if (!PCPSUtil.isEmpty(menuName)) {
			cond += " and menuName like '%" + menuName + "%'";
		}
		if (!PCPSUtil.isEmpty(menuType)) {
			cond += " and menuType ='" + menuType + "'";
		}
		Query query = _em
				.createNativeQuery("select nvl(count(*),0) from SysOpMenu t where 1=1"
						+ cond);
		return new BigInteger(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	public List<SysOpMenu> getSysOpMenuList(String menuName, String menuType,
			Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String cond = "";
		if (!PCPSUtil.isEmpty(menuName)) {
			cond += " and menuName like '%" + menuName + "%'";
		}
		if (!PCPSUtil.isEmpty(menuType)) {
			cond += " and menuType ='" + menuType + "'";
		}
		Query query = _em
				.createNativeQuery(
						"select * from SysOpMenu t where 1=1 "+cond+" order by menutype,supermenuid,menuseqno",
						SysOpMenu.class).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeftMenu> getSysOpMenuListByRoleId(String roleIds) {
		String sql = "select c.menuid,c.menuname,c.menuseqno,c.menutype,c.supermenuid,a.sapurl from sysopsap a,sysopmenu c,(select distinct(sapid) from sysopacl b where b.roleid in ("+roleIds+")) d where a.sapid=d.sapid and a.menuid=c.menuid order by c.supermenuid,c.menuseqno";
		Query query = _em.createNativeQuery(sql, LeftMenu.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpMenu> getSysOpMenuListBySuperMenuId(Integer superMenuId) {
		String jpql = "select * from sysopmenu t where t.supermenuid=?1 order by menuId";
		Query query = _em.createNativeQuery(jpql, SysOpMenu.class)
				.setParameter(1, superMenuId);
		return query.getResultList();
	}

	// 新增
	@Override
	public void insertSysOpMenu(SysOpMenu sysOpMenu)
			throws PCPSApplicationException {
		sysOpMenu.setMenuId(getMaxMenuId());
		GeneralDao.insert(_em, sysOpMenu);
	}

	// 新增下拉选择
	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpMenu> getSysOpMenuByMenuType(Integer menutype) {
		Query query = _em
				.createNativeQuery(
						"select * from sysopmenu t where t.menutype=?1 order by t.supermenuid,t.menuseqno",
						SysOpMenu.class).setParameter(1, menutype);
		return query.getResultList();
	}

	// 删除
	@Override
	public void deleteSysOpMenu(Integer menuId) throws PCPSApplicationException {
		SysOpMenu eo = getSysOpMenuByMenuId(menuId);
		if (eo != null) {
			GeneralDao.remove(_em, eo);
		}
	}

	// 修改
	@Override
	public void updateSysOpMenu(SysOpMenu sysOpMenu)
			throws PCPSApplicationException {
		SysOpMenu _sysOpMenu = getSysOpMenuByMenuId(sysOpMenu.getMenuId()
				.intValue());
		if (_sysOpMenu == null) {
			throw new OrganApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "待修改菜单数据库中不存在!");
		}
		// 对象赋值
		_sysOpMenu.setMenuName(sysOpMenu.getMenuName());
		_sysOpMenu.setMenuType(sysOpMenu.getMenuType());
		_sysOpMenu.setMenuSeqNo(sysOpMenu.getMenuSeqNo());
		_sysOpMenu.setSuperMenuId(sysOpMenu.getSuperMenuId());
		// 修改
		GeneralDao.update(_em, _sysOpMenu);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeftParentMenu> getSysOpMenuListByRoleIdAndMenuGrpId(
			String roleIds, BigInteger menuGrpId) {
		String sql = "select x.menuid,x.menuname from sysopmenu x,(select distinct(c.supermenuid) supermenuid from "
				+ "sysopsap a,sysopacl b,sysopmenu c where a.sapid=b.sapid and a.menuid=c.menuid and b.roleid in ("+roleIds+")) y "
				+ "where x.menuid=y.supermenuid and x.supermenuid=?1 order by x.menuseqno";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, menuGrpId);
		List<Object[]> result = query.getResultList();
		List<LeftParentMenu> resultList = new ArrayList<LeftParentMenu>();
		LeftParentMenu lpm = null;
		for (int i = 0; i < result.size(); i++) {
			Object[] record = result.get(i);
			lpm = new LeftParentMenu();
			lpm.setMenuId(((BigDecimal) record[0]).intValue());
			lpm.setMenuName((String) record[1]);
			resultList.add(lpm);
		}
		return resultList;
	}
}
